﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaiGaoQiaoWeb.JsonEntities;
using WaiGaoQiaoWeb.Models;
using Model.WaiGaoQiao;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class PostController : Controller
    {
        Work_Post_Relation work = new Work_Post_Relation(true);
        Work_ArticleInfo wa = new Work_ArticleInfo();
        Work_post wp = new Work_post();
        Work_ArticleRuleInfo rule = new Work_ArticleRuleInfo(true);
        public ActionResult PostIndex()
        {
            return View();
        }
        /// <summary>
        /// 获得岗位名称
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RtnPostName(int Id)
        {
            return Json(wp.GetPostName(Id));
        }
        /// <summary>
        ///  获得物品名称
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RtnArticleName(int Id)
        {
            return Json(wa.GetCon_ArticleById(Id).ArticleName);
        }
        /// <summary>
        /// 验证是否定义这个规格
        /// </summary>
        /// <param name="postId"></param>
        /// <param name="articleId"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Validate(int postId,int articleId)
        {
          return Json(work.GetWork_PostCount("and postid=" + postId + " and articleId=" + articleId + "") > 0 ? false : true);
        }

        /// <summary>
        /// 获取规则列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetRuleType()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(0, "请选择...");
            foreach (Work_ArticleRuleInfo r in rule.GetAllArticleRule("").OrderBy(model => model.Month))
            {
                dic.Add(r.ID, r.ArticleName);
            }
            IList<SelectListItem> selectList =
             dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }


        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetPostList(PageVM pvm)
        {
            string condition = pvm.query;
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = work.GetWork_PostCount(condition);
            IList<Work_Post_Relation> list = work.GetAllPost(condition, pvm.rp, pvm.page);
            List<string> v;
            foreach (Work_Post_Relation ida in list)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(wp.GetPostName(ida.PostId));
                v.Add(wa.GetCon_ArticleById(ida.ArticleId).ArticleName);
                v.Add(rule.GetAllArticleRule(ida.RuleId).ArticleName);
                v.Add(ida.GiveCount.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }
        /// <summary>
        /// 转到添加界面或者修改界面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddPost(int? id)
        {
            PostVM idvm = new PostVM();
            ViewBag.Rule = GetRuleType();
            if (!id.HasValue)
            {
                idvm.ID = 0;
            }
            else
            {

                Work_Post_Relation idinfo = work.GetAllPost(Convert.ToInt32(id));
                Mapper.DynamicMap(idinfo, idvm);
                idvm.Work_postStructrueId = idinfo.PostId;
                idvm.Work_postStructrueId1 = idinfo.PostId;
                idvm.Work_ArticleInfoID = idinfo.ArticleId;
                idvm.Work_ArticleInfoID1 = idinfo.ArticleId;
            }
            return View(idvm);
        }
        /// <summary>
        /// 删除物品领用规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(int id)
        {
            return Json(work.DeletePost("Id="+id));
        }
        /// <summary>
        /// 保存物品领用规则
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveWork_Post(PostVM vm)
        {
            if (ModelState.IsValid)
            {
                Work_Post_Relation info = new Work_Post_Relation();
                vm.PostId = vm.Work_postStructrueId;
                vm.ArticleId = vm.Work_ArticleInfoID;
                Mapper.DynamicMap(vm, info);
                work.SaveWork_Post(info);
                return Json(true);
            }
            else
            {
                return Json(false);
            }
        }
    }
}
